package com.matrix.toolutils.common;

import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast

/**
 * 作者：Administrator
 * 日期：2024/9/29 11:36
 * 描述：
 */
class ToastUtils(context: Context?) : Toast(context) {


    /**
     * 自定义Toast样式
     *
     * @param context
     * @param resId
     * @param text
     * @param duration
     * @description
     */
    private fun makeText(context: Context, resId: Int, text: CharSequence, duration: Int): Toast {
        val result = Toast(context)

        // 由layout文件创建一个View对象
        val layout = LinearLayout(context)
//        layout.setBackgroundResource(R.drawable.toast_background)
        layout.setPadding(30, 10, 30, 10)
        // 实例化ImageView和TextView对象
        val imageView = ImageView(context)
        val textView = TextView(context)
        layout.addView(imageView)
        layout.addView(textView)

        //这里我为了给大家展示就使用这个方面既能显示无图也能显示带图的toast
        //不用显示图片直接写0，显示图片加载ID即可
        if (resId == 0) {
            imageView.visibility = View.GONE
        } else {
            imageView.setImageResource(resId)
        }
        textView.text = text
        textView.setTextColor(Color.WHITE)
        result.view = layout
        result.setGravity(Gravity.CENTER_VERTICAL, 0, 0)
        result.duration = duration
        return result
    }

    private fun makeSelfText(context: Context, layout: View, duration: Int): Toast {
        val result = Toast(context)
        result.view = layout
        result.setGravity(Gravity.CENTER, 0, 0)
        result.duration = duration
        return result
    }

    /**
     * @param context    上下文
     * @param imageResId 图片Id
     * @param message    消息
     * 默认3秒
     */
    fun showToast(context: Context, imageResId: Int, message: String) {
        val mToast: Toast = makeText(context, imageResId, message, 3000)
        mToast.show()
    }

    /**
     * @param context 上下文
     * @param message 消息
     * 没图片 0
     */
    fun showToast(context: Context, message: String) {
        val mToast: Toast = makeText(context, 0, message, 3000)
        mToast.show()
    }

    fun showSysToast(context: Context?, message: String?) {
        val mToast = Toast.makeText(context, message, Toast.LENGTH_SHORT)
        mToast.show()
    }

    fun showSelfToast(context: Context, layout: View) {
        val mToast = makeSelfText(context, layout, Toast.LENGTH_SHORT)
        mToast.show()
    }


    fun showToast(context: Context, imageResId: Int, messageResId: Int) {
        val mToast: Toast = makeText(
            context,
            imageResId,
            context.resources.getString(messageResId),
            3000
        )
        mToast.show()
    }

    /**
     * @param context
     * @param messageResId 消息String ID
     */
    fun showToast(context: Context, messageResId: Int) {
        val mToast: Toast = makeText(
            context,
            0,
            context.resources.getString(messageResId),
            3000
        )
        mToast.show()
    }


    /**
     * @param messageResId 消息String ID
     */
    fun showToastTime(context: Context, messageResId: Int, duration: Int) {
        val mToast: Toast = makeText(
            context,
            0,
            context.resources.getString(messageResId),
            duration
        )
        mToast.show()
    }
}